package com.jh.fcsm.service.sys.impl;

import java.util.List;
import org.springframework.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jh.fcsm.beans.sys.SysDatasource;
import com.jh.fcsm.beans.sys.vo.SysDatasourceVo;
import com.jh.fcsm.common.exception.ServiceException;
import com.jh.fcsm.common.BaseServiceImpl;
import com.jh.fcsm.util.security.UUIDUtils;
import com.jh.fcsm.dao.sys.SysDatasourceMapper;
import com.jh.fcsm.service.sys.SysDatasourceService;
import com.jh.fcsm.constant.CommonConstant;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 *  数据源Service业务层处理
 * 
 * @author linq
 * @date 2023-08-11
 */
@Service
@Transactional(readOnly = true)
public class SysDatasourceServiceImpl extends BaseServiceImpl<SysDatasourceMapper, SysDatasource> implements SysDatasourceService{
	
	private static final Logger logger = LoggerFactory.getLogger(SysDatasourceServiceImpl.class);
    @Autowired
    private SysDatasourceMapper sysDatasourceMapper;

	@Override
	@Transactional(readOnly = false)
	/**
	 * 保存或更新数据源
	 *@param sysDatasource 数据源对象
	 *@return String 数据源ID
	 *@author linq
	 */
	public String saveOrUpdateSysDatasource(SysDatasource sysDatasource) {
		if(sysDatasource==null){
			throw new ServiceException("数据异常");
		}
		if(StringUtils.isEmpty(sysDatasource.getId())){
			//新增
			sysDatasource.setId(UUIDUtils.getUUID());
			sysDatasourceMapper.insertSelective(sysDatasource);
		}else{
			//避免页面传入修改
			sysDatasource.setYn(null);
			sysDatasourceMapper.updateByPrimaryKeySelective(sysDatasource);
		}
		return sysDatasource.getId();
	}

	@Override
	@Transactional(readOnly = false)
	/**
	 * 删除数据源
	 *@param ids void 数据源ID
	 *@author linq
	 */
	public void deleteSysDatasource(List<String> ids) {
		for(String id:ids){
			//TODO 做判断后方能执行删除
			SysDatasource sysDatasource=sysDatasourceMapper.selectByPrimaryKey(id);
			if(sysDatasource==null){
				throw new ServiceException("非法请求");
			}
			//逻辑删除
			SysDatasource temsysDatasource=new SysDatasource();
			temsysDatasource.setYn(CommonConstant.FLAG_NO);
			temsysDatasource.setId(sysDatasource.getId());
			sysDatasourceMapper.updateByPrimaryKeySelective(temsysDatasource);
		}
	}

	/**
	 * 查询数据源详情
	 *@param id
	 *@return SysDatasource
	 *@author linq
	 */
    @Override
	public SysDatasource findById(String id) {
		return sysDatasourceMapper.selectByPrimaryKey(id);
	}


	/**
	 * 分页查询数据源
	 *@param sysDatasourceVo
	 *@return PageInfo<SysDatasource>
	 *@author linq
	 */
	@Override
	public PageInfo<SysDatasource> findPageByQuery(SysDatasourceVo sysDatasourceVo) {
		PageHelper.startPage(sysDatasourceVo.getPageNum(),sysDatasourceVo.getPageSize());
		Example example=new Example(SysDatasource.class);
		Criteria criteria=example.createCriteria();
		criteria.andEqualTo("yn",CommonConstant.FLAG_YES);
		//查询条件
		//if(!StringUtils.isEmpty(sysDatasourceVo.getName())){
		//	criteria.andEqualTo(sysDatasourceVo.getName());
		//}
		List<SysDatasource> sysDatasourceList=sysDatasourceMapper.selectByExample(example);
		return new PageInfo<SysDatasource>(sysDatasourceList);
	}
}
